using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Keep.Service;
using Serilog;
using Keep.Model;
using Keep.Const;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class FileController : ControllerBase
{

    private readonly IFileService fileService;

    public FileController(IFileService fileService)
    {
        this.fileService = fileService;
    }

    [HttpPost(Name = "FileUpload")]
    [RequestSizeLimit(long.MaxValue)]
    public IActionResult FileUpload(List<IFormFile> file)
    {
        try
        {
            if (file.Count > 0)
            {
                // Log.Warning("Upload => " + path);
                fileService.UploadFile(file, "2", Static.FilePath);
                return Ok(new { file.Count, Size = fileService.SizeConverter(file.Sum(f => f.Length)) });
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }

    [HttpGet(Name = "Download")]
    public IActionResult Download([Required] string subDirectory)
    {
        try
        {
            Log.Warning("IN => " + subDirectory);
            var (fileType, archiveData, archiveName) = fileService.DownloadFiles(subDirectory);
            return File(archiveData, fileType, archiveName);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

    // [HttpGet(Name = "GetFilePathInfo")]
    // public async Task<ActionResult<ResultData>> GetFilePathInfo(string? path)
    // {
    //     Log.Warning("IN => " + path);
    //     List<FileDiy> sys = await Task.Run(() => fileService.GetPathInfo(Static.FilePath, path, ""));
    //     Log.Warning("OUT => " + sys.ToString());
    //     return Ok(new ResultData(Static.Enable, sys));
    // }

    // [HttpGet(Name = "GetFileData")]
    // public async Task<ActionResult<ResultData>> GetFileData(string path)
    // {
    //     Log.Warning("IN => " + path);
    //     string data = await Task.Run(() => fileService.GetFileData(Static.BlogPath, path));
    //     Log.Warning("OUT => " + data);
    //     return Ok(new ResultData(Static.Enable, data));
    // }

}
